# Maintainer: Alexey Kasatkin <alexeikasatkin@gmail.com>

_realname=libosmpbf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=1.5.0
pkgrel=1
pkgdesc="A library to support OpenStreetMap's protocolbuffer binary .pbf format (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/scrosby/OSM-binary"
license=('LGPL3')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
depends=("${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-zlib")
options=('staticlibs' 'strip')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/openstreetmap/OSM-binary/archive/v${pkgver}.tar.gz"
        001-fix-linking.patch)
sha256sums=('2abf3126729793732c3380763999cc365e51bffda369a008213879a3cd90476c'
            'f175ae52f195a93da90cde5c4b249d0b3c7d3fbb95bddbc2ea0aafd3677aafe6')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare() {
  plain "Extracting ${_realname}-${pkgver}.tar.gz due to symlink(s) without pre-existing target(s)"
  [[ -d ${srcdir}/OSM-binary-${pkgver} ]] && rm -rf ${srcdir}/OSM-binary-${pkgver}
  tar -xzvf ${srcdir}/${_realname}-${pkgver}.tar.gz -C ${srcdir} || true

  cd ${srcdir}/OSM-binary-${pkgver}
  patch -p1 -i ${srcdir}/001-fix-linking.patch

  cp -rf ${srcdir}/OSM-binary-${pkgver}/{src,osmpbf}
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    ../OSM-binary-${pkgver}

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
  install -Dm644 ${srcdir}/OSM-binary-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
